package com.ml.service.impl;

import com.ml.dao.BaseDao;
import com.ml.model.MLUser;
import com.ml.service.HrmDepartMentService;
import com.ml.service.MLUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Transactional
@Service()
public class MLUserServiceImpl implements MLUserService {
    @Autowired
    private BaseDao<MLUser> basedao;
    @Autowired
    private HrmDepartMentService hrmDepartMentService;




    /**根据分部 locationid 获取用户列表，排除离职等，并且包涵卡号和业务主管等信息 */
    public List<Object[]> getListByLocation0(Integer locationid){
        String sql = " SELECT c.id as id ,c.field0 as xzzg,c.field1 as kqkh  , h.loginid as loginid ,h.lastname as lastname " +
                " FROM cus_fielddata c , hrmresource h where c.id=h.id and h.locationid="+locationid+" and h.status<4  and c.scopeid=-1";
        return  basedao.getBySqlObject(sql);
    }

    //所有人
    public List<MLUser> getAll(){
        String hql  = " from MLUser ";
        return  basedao.find(hql,new Object[]{});
    }
    //所有在职的人
    public List<MLUser> getAllOnTheJob(){
        String hql  = " from MLUser where status < 4  ";
        return  basedao.find(hql,new Object[]{});
    }

    /**根据分部获取用户列表*/
    public List<MLUser> getListByLocation1(Integer locationid){
        String hql  = " from MLUser where locationid = ? and status < 4  ";
        return  basedao.find(hql,new Object[]{locationid});
    }


    /**根据分部获取用户列表*/
    public List<MLUser> getListByLocation2(Integer locationid){
        String hql  = " from MLUser where locationid = ? ";
        return  basedao.find(hql,new Object[]{locationid});
    }



    /**通过用户名和密码获取用户*/
    public MLUser getByUserNameAndPsd(String username, String password){
        String hql  = " from MLUser where loginid = ? and password = ? ";
        List<MLUser> list =  basedao.find(hql,new Object[]{username,password});
        if(list!=null && list.size()>0){
            return list.get(0);
        }else {
            return null ;
        }
    }







    /**根据ids查询参数一定是 1,2,3的形式*/
    public List<MLUser> getListByIds(String ids){
        if(!StringUtils.hasText(ids)){
            ids = "0";
        }
        String hql  = " from MLUser where id in ("+ids+") ";
        return  basedao.find(hql);
    }

    /**根据部门ID获取用户列表，考虑分级部门*/
    public List<MLUser> getListByDepartid(String departid){
        String departStr = hrmDepartMentService.getByAllNextDepartStr2(Integer.valueOf(departid));
        String hql = "  from MLUser where departmentid in ("+departStr+") order by id ";
        List<MLUser> users = basedao.find(hql);
       return  users;
    }

    /**根据项目的code获取order下所有相关人员 ids */
    public List<MLUser> getListByXmdm(String xmdm){
        String sql = " select * from hrmresource where FIND_IN_SET(id,(select  group_concat(orderry) from uf_order WHERE ssxm in  ( SELECT id from uf_project WHERE xmdm = ? )))  order by id  ; ";
        return  basedao.getBySql(sql,new Object[]{xmdm},MLUser.class);
    }

    /**
     * 根据项目的code获取order下所有相关人员
     * 相关人员的ID
     * */
    public List<MLUser> getListByXmdm(String xmdm, String userid){
        String sql = " select * from hrmresource where FIND_IN_SET(id,(select  group_concat(orderry) from uf_order WHERE ssxm in  ( SELECT id from uf_project WHERE xmdm = ? ) and  (   FIND_IN_SET(?,xmjl) or  FIND_IN_SET(?,zzr) or  FIND_IN_SET(?,zzrzl)  )  ))  order by id  ; ";
        return  basedao.getBySql(sql,new Object[]{xmdm,userid,userid,userid},MLUser.class);
    }


    @Override
    public List<Object[]> getSqlByID(String id) {
        //这里暂时不考虑sql注入问题
        String sql = " select * FROM hrmresource where id ="+id;//
        return null;
    }

    @Override
    public List<MLUser> getList(MLUser model) {
        return null;
    }

    @Override
    public List<MLUser> getListByPage(Integer page, Integer rows) {
        return null;
    }

    @Override
    public MLUser getModelByParam(MLUser model) {
        return null;
    }

    @Override
    public void delete(String id) {

    }

    @Override
    public void saveOrUpdate(MLUser model) {

    }

    @Override
    public MLUser getByID(Integer id) {
        return basedao.get(MLUser.class , id );
    }

    @Override
    public void getList(MLUser model, HttpServletRequest request, HttpServletResponse response) {

    }
}
